/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Z.YD
 * Date: 2025-07-15
 * Time: 10:07
 */
public class MySingleLinkedList {
    class ListNode {
        int val;
        ListNode next;
        ListNode (int val) {
            this.val = val;
        }
    }
    public ListNode head;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null) {
            head = node;
            return;
        }
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        int len = size();
        if(index < 0 || index > len) {
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode node = new ListNode(data);
        while(index - 1 !=0) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return  false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if ( head == null) {
            return;
        }
        if( head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = findKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    //查找结点
    public ListNode findKey(int key) {
        ListNode cur = head;
        while(cur.next!= null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur!=null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
    public void display() {
        ListNode cur = head;
        while(cur!=null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
